const fs = require("fs");

const list = [
    { name: '张1', gender: "女" },
    { name: '张2', gender: "男" },
    { name: '张3', gender: "女" },
    { name: '张4', gender: "男" },
    { name: '张5', gender: "女" },
    { name: '张6', gender: "男" },
]

let list3 = [], list2 = []

getDate(list);
console.log(list2, list3);
// 封装复用
function getDate(arr) {
    arr.map((value, key) => {
        if (value.gender === "男") {
            list2.push(arr[key])
        } else {
            list3.push(arr[key])
        }
    });

    fs.writeFileSync("./data/男.json", `${JSON.stringify(list2)}`)
    fs.writeFileSync("./data/女.json", `${JSON.stringify(list3)}`)
}

const list1 = [
    { name: "悟空", gender: "男" },
    { name: "悟饭", gender: "男" },
    { name: "菩萨", gender: "女" },
    { name: "白骨精", gender: "女" }
];
getDate(list1);
console.log(list2, list3);

// 方式一
/* const list = [
    { name: '张1', gender: "女" },
    { name: '张2', gender: "男" },
    { name: '张3', gender: "女" },
    { name: '张4', gender: "男" },
    { name: '张5', gender: "女" },
    { name: '张6', gender: "男" },
]

let list3 = [], list2 = []

list2 = list.filter(item => item.gender === "男");
list3 = list.filter(item => item.gender === "女");

const list1 = [
    { name: "悟空", gender: "男" },
    { name: "悟饭", gender: "男" },
    { name: "菩萨", gender: "女" },
    { name: "白骨精", gender: "女" }
];

list1.map((value, key) => {

    if (value.gender === "男") {
        list2.push(list1[key])
    } else {
        list3.push(list1[key])
    }
}) 
console.log(list2, list3);
fs.writeFileSync("./data/男.json", `${JSON.stringify(list2)}`)
fs.writeFileSync("./data/女.json", `${JSON.stringify(list3)}`)
*/

